﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.DAL;
using ExtAspNet;

namespace MyDMS_ExtJS
{
    public partial class Index : PageBase
    {
        private MyDMSEntity _myDmsEntity=new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTheUD(Author.user_Code);
            }
        }


        private void BindTheUD(int userCode)
        {
            gvUploadedDocs.SortColumnIndex = 0;
            gvUploadedDocs.SortDirection = "ASC";

            gvUploadedDocs.DataSource = from d in _myDmsEntity.Document.Where(ud => ud.Owner == userCode)
                                        select new
                                        {
                                            d.Document_Name,
                                            d.Document_Desc,
                                            d.Document_Code,
                                            d.Updation_Date,
                                            TaskName =
                                 _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == d.Task_Code).Task_Title,
                                            FolderName =
                                 _myDmsEntity.Folder.FirstOrDefault(f => f.Folder_Code == d.Folder_Code).
                                 Folder_Title,
                                 SecretLevelName=_myDmsEntity.SecretLevel.FirstOrDefault(sl=>sl.SecretLevel_Code==d.SecretLevel_Code).SecretLevel_Name,
                                 ShelfLifeName=_myDmsEntity.ShelfLife.FirstOrDefault(s=>s.ShelfLife_Code==d.ShelfLife_Code).ShelfLife_Name
                                        };
            gvUploadedDocs.DataBind();
        }

        protected void gvUploadedDocs_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            var query = from d in _myDmsEntity.Document.Where(ud => ud.Owner == Author.user_Code)
                                        select new
                                        {
                                            d.Document_Name,
                                            d.Document_Desc,
                                            d.Document_Code,
                                            d.Updation_Date,
                                            TaskName =
                                 _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == d.Task_Code).Task_Title,
                                            FolderName =
                                 _myDmsEntity.Folder.FirstOrDefault(f => f.Folder_Code == d.Folder_Code).
                                 Folder_Title,
                                            SecretLevelName = _myDmsEntity.SecretLevel.FirstOrDefault(sl => sl.SecretLevel_Code == d.SecretLevel_Code).SecretLevel_Name,
                                            ShelfLifeName = _myDmsEntity.ShelfLife.FirstOrDefault(s => s.ShelfLife_Code == d.ShelfLife_Code).ShelfLife_Name
                                        };
            DataTable table = query.CopyToDataTable();
            DataView  view = table.DefaultView;
            view.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            gvUploadedDocs.DataSource = view;
            gvUploadedDocs.DataBind();
        }


        protected void gvUploadedDocs_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gvUploadedDocs.PageIndex = e.NewPageIndex;
        }
    }
}